import { Router } from 'vue-router'
import { checkAuth } from '@/utils'
import { configSetting } from '@/config'
import { db } from '@/storage'
import { start } from '@/utils'

export const useRouterAuth = (router: Router) => {
  router.beforeEach((to, from, next) => {
    start()
    const token = db.get('token')
    const name = to.name as string
    const auth = to.meta?.notAuth
    if (auth || checkAuth(name)) {
      next()
    } else {
      if (token) next({ name: configSetting.NotAuth403 })
      else next({ name: configSetting.LoginMainLayoutName })
    }
  })
}
